package com.itheima.servlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    实现查看功能
 */
@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:/stu.txt"));

        List<Student> stus = new ArrayList<>();

        String line = null;
        while((line = br.readLine()) != null){
            //xx,xx,xx
            String[] values = line.split(",");
            Student stu = new Student(values[0], Integer.parseInt(values[1]), Integer.parseInt(values[2]));
            stus.add(stu);
        }

        br.close();

        req.setAttribute("stus",stus);

        req.getRequestDispatcher("/listStudent.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
